package com.pig.service;

import com.alibaba.dubbo.config.annotation.Service;

import com.pig.dao.GoodsDao;
import com.pig.exception.NoDataFoundException;
import com.pig.exception.UnExceptDataException;
import com.pig.pojo.goods.Goods;
import com.pig.service.goods.GoodsService;
import com.pig.util.PageObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(timeout = 120000)
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsDao goodsDao;

    @Override
    public PageObject<Goods> findGoodsByTitle(String title, Integer status, Integer categoryId, Integer pageCurrent,Integer pageSize) {
        //判断页码是否存在
        if(pageCurrent == null || pageCurrent <= 0){
            throw new UnExceptDataException("页码传输错误！");
        }
        //查询数据库是否存在数据
        int rowCount = goodsDao.findGoodsCount(title, categoryId);
        //不存在 抛出异常
        if (rowCount == 0) {
            throw new NoDataFoundException("未查询到相关数据！");
        }
        //存在，查询详细信息
        List<Goods> goodsList = goodsDao.findGoodsByTitle(title, (pageCurrent - 1) * pageSize, pageSize, status, categoryId);
        System.out.println(goodsList);
        if (goodsList == null || goodsList.size() == 0) {
            throw new NoDataFoundException("查询出现异常!请重试");
        }
        return new PageObject<>(pageSize, rowCount, pageCurrent, goodsList);
    }

    @Override
    public Map<String, Object> findGoodsById(Integer goodsId) {
        Map<String, Object> goodsById = goodsDao.findGoodsById(goodsId);
        return goodsById;
    }

    @Override
    public PageObject findGoodsPages(String title, Integer pageCurrent,Integer pageSize) {
        int rowCount = goodsDao.findGoodsCount(title, -1);
        return new PageObject(pageSize, rowCount, pageCurrent, null);
    }

    @Override
    public void delGoods(Integer goodsId) {
        goodsDao.deleteById(goodsId);
    }

    @Override
    public int setGoodsStatus(Integer goodsId, Integer status) {
        if (goodsId == null || goodsId <= 0) {
            throw new UnExceptDataException("ID不正确，请重试！");
        }
        if (status != 0 && status != 1) {
            throw new UnExceptDataException("状态码不正确！");
        }
        return goodsDao.setGoodsStatus(goodsId, status);
    }

    /**
     * 更新/添加商品时校验商品的方法
     * @param goods
     * @return
     */
    private void checkGoodsData(Goods goods) {
        if (StringUtils.isEmpty(goods))
            throw new UnExceptDataException("发生异常！请重试");
        if (StringUtils.isEmpty(goods.getTitle()))
            throw new UnExceptDataException("商品名称不可为空！");
        if (StringUtils.isEmpty(goods.getCategoryId()))
            throw new UnExceptDataException("请选择商品所属目录！");
        if (StringUtils.isEmpty(goods.getDes()))
            throw new UnExceptDataException("商品描述不可为空！");
        if (StringUtils.isEmpty(goods.getPrice())||goods.getPrice() <= 0)
            throw new UnExceptDataException("商品价格错误！");
        if (StringUtils.isEmpty(goods.getOrigin()))
            throw new UnExceptDataException("商品产地不可为空！");
        if (StringUtils.isEmpty(goods.getWeight()))
            throw new UnExceptDataException("商品重量不可为空！");
        if (StringUtils.isEmpty(goods.getStorage()))
            throw new UnExceptDataException("请填写商品保质期！");
        if (StringUtils.isEmpty(goods.getStorageFactor()))
            throw new UnExceptDataException("请填写存储条件！");
    }

    @Override
    public int insertGoods(Goods goods) {
        goods.setId(null);
        checkGoodsData(goods);
        goods.setCreatedTime(new Date());
        goods.setModifiedTime(new Date());
        return goodsDao.insert(goods);
    }

    @Override
    public void UpdateGoods(Goods goods) {
        checkGoodsData(goods);
        goods.setModifiedTime(new Date());
        goodsDao.updateById(goods);
    }


    @Override
    public List<Goods> getRandomGoods() {
        return goodsDao.findRandomGoods();
    }

    @Override
    public List<Goods> selectItemByItemId(Integer itemId) {
        return null;
    }
}
